package com.mz.ozy.activity.assets;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.mvp.presenter.AddressManagerPresenter;
import com.mz.ozy.mvp.view.AddressManagerContract;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.widget.dialog.PayPopupWindow;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddressManagerActivity extends BaseActivity<AddressManagerContract.View, AddressManagerPresenter> implements AddressManagerContract.View {

    @BindView(R.id.addressEt)
    EditText mAddressEt;
    @BindView(R.id.remarkEt)
    EditText mRemarkEt;
    @BindView(R.id.confirmBt)
    Button mConfirmBt;
    @BindView(R.id.tv_coins_type)
    TextView mTvCoinsType;
    @BindView(R.id.et_paypwd)
    EditText etPaypwd;
    private String coinCode;
    private String address;
    private PayPopupWindow mPayPopupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_manager);
        ButterKnife.bind(this);
        initTitle();
        address = getIntent().getStringExtra("address");
        tvCenterTitle.setText("USDT提币地址");
        mTvCoinsType.setText(getString(R.string.tb_address));
        mRemarkEt.setText(getIntent().getStringExtra("remark"));
        mAddressEt.setText(address);
    }

    @OnClick(R.id.confirmBt)
    public void onViewClicked() {
        if (TextUtils.isEmpty(mAddressEt.getText().toString().trim())) {
            ToastUtils.showToast(getString(R.string.str_input_tb_address));
            return;

        }
        if (!mAddressEt.getText().toString().trim().startsWith("0x") || mAddressEt.getText().toString().trim().length()<42 || Utils.isContainChinese(mAddressEt.getText().toString().trim())) {
            ToastUtils.showToast(getString(R.string.str_address_format_error));
            return;
        }
        if (TextUtils.isEmpty(etPaypwd.getText().toString().trim())) {
            ToastUtils.showToast(getString(R.string.input_pay_pwd_hint));
            return;
        }
        mPresenter.saveAddres(AddressManagerActivity.this, etPaypwd.getText().toString().trim(), "", mAddressEt.getText().toString().trim(), mRemarkEt.getText().toString().trim());
    }

    public void showPaypwdPop() {
        if (mPayPopupWindow == null) {
            mPayPopupWindow = new PayPopupWindow(this, new PayPopupWindow.OnClickListener() {
                @Override
                public void onConfirm(String pwd) {
                    mPresenter.saveAddres(AddressManagerActivity.this, pwd, coinCode, mAddressEt.getText().toString().trim(), mRemarkEt.getText().toString().trim());

                }
            });
        }
        mPayPopupWindow.showAtLocation(titleBar, Gravity.BOTTOM, 0, 0);


    }


    @Override
    public void saveAddressuccess(Object o) {
        ToastUtils.showToast(getString(R.string.str_save_tb_address_success));
        Intent intent = new Intent();
        intent.putExtra("address", mAddressEt.getText().toString().trim());
        intent.putExtra("remark", mRemarkEt.getText().toString().trim());
        setResult(100, intent);
        finish();


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
